// Modules to control application life and create native browser window
const { app, Menu, Tray, ipcMain, BrowserWindow, clipboard, BrowserView } = require('electron');
const {spawn} = require('child_process');
const path = require('path');
const ViewManager = require('./views');


//获取单实例锁
const appLock = app.requestSingleInstanceLock()
if (!appLock) {
  app.quit();
}

let mainWindow = null;
let viewManager = null;

function createWindow() {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    frame: false,
    show: false,
    webPreferences: {
      webSecurity: false,
      nodeIntegration: false,
      preload: path.join(__dirname, 'preload.js')
    }
  })

  viewManager = new ViewManager(mainWindow);
  //mainWindow.loadFile(path.join(__dirname, 'index.html') )
  mainWindow.loadURL('http://127.0.0.1:8000/')

  //界面大小调整
  mainWindow.on('resize', () => {
    viewManager.resizeView();
  })

  mainWindow.once('ready-to-show', () => {
    mainWindow.show();
    mainWindow.webContents.openDevTools();
  })
}




// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  createWindow()

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.


//IPC 处理
ipcMain.handle('/api/getPages', async (event, args) => {
  let data = viewManager.getPages();
  return {
    code: 0,
    data
  }
});

ipcMain.handle('/api/getWindowState', async (event, args) => {
  let windowState = '';

  if (mainWindow.isNormal()) {
    windowState = 'normal';
  }
  else if (mainWindow.isMaximized()) {
    windowState = 'maximize';
  }
  else if (mainWindow.isMinimized()) {
    windowState = 'minimize';
  }
  return {
    windowState
  }
});

ipcMain.handle('/api/quit', async (event, args) => {
  viewManager.closeAllPageViews();
  mainWindow.close();
});

ipcMain.handle('/api/minimize', async (event, args) => {
  mainWindow.minimize();
  return {
    windowState: 'minimize'
  }
});


ipcMain.handle('/api/maximize', async (event, args) => {
  let windowState = 'normal';
  if (!mainWindow.isMaximized()){
    mainWindow.maximize();
    windowState = 'maximize';
  }
  else{
    mainWindow.restore();
  }
  viewManager.resizeView();
  return {
    windowState
  }
});

ipcMain.handle('/api/restore', async (event, args) => {
  mainWindow.restore();
  return {
    windowState: 'normal'
  }
});

 

ipcMain.handle('/api/newPage', async (event, args) => {
  let view = viewManager.newPage();
  return {
    windowId:view.id
  }
});

ipcMain.handle('/api/changePage', async (event, args) => {
  viewManager.changePage(args.windowId);
  return {
    activeWindowId:viewManager.activeWindowId()
  }
});

ipcMain.handle('/api/closePage', async (event, args) => {
  viewManager.closePage(args.windowId);
});

ipcMain.handle('/api/openUrl', async (event, args) => {
  try {
    let view = await viewManager.openUrlInPage(args.url);
    return {
      windowId:view.id,
      title: view.webContents.getTitle(),
      url:view.webContents.getURL()
    };
  } catch (error) {
    console.log(error);
  }
  
});

ipcMain.handle('/api/openDev', async (event, args) => {
  viewManager.openDev();
});

ipcMain.handle('/api/capture',async(event,args)=>{
  spawn('rundll32',['PrScrn.dll','PrScrn']);
})

ipcMain.handle('/api/reload',async(event,args)=>{
  viewManager.reload();
})
ipcMain.handle('/api/goBack',async(event,args)=>{
  try {
    viewManager.goBack();
  } catch (error) {
    console.log(error);
  }
  
})
ipcMain.handle('/api/goForward',async(event,args)=>{
  viewManager.goForward();
})